package com.igeek.health.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.health.constant.MessageConstant;
import com.igeek.health.constant.RedisConstant;
import com.igeek.health.entity.Checkitem;
import com.igeek.health.entity.Setmeal;
import com.igeek.health.result.Result;
import com.igeek.health.service.ISetmealService;
import com.igeek.health.utils.QiniuUtils;
import com.igeek.health.utils.RedisUtils;
import com.igeek.health.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Set;
import java.util.UUID;

/**
 * @Author fengqq
 * @Description TODO
 * @Date 2024/8/28  14:39
 */

@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐模块的控制层")
public class SetmealController {

    @Autowired
    private ISetmealService setmealService;

    @Autowired
    private RedisUtils redisUtils;

    @ApiOperation("上传套餐图片")
    @PostMapping("/upload")
    public Result upload(@RequestPart("imgFile") MultipartFile imgFile) throws IOException {
        if(ObjectUtil.isNotEmpty(imgFile)){
            //获取原始文件扩展名
            String oldName = imgFile.getOriginalFilename();
            //生成新的文件名  yyyy/MM/dd/xxx.jpg
            String newName = UUID.randomUUID()+oldName.substring(oldName.lastIndexOf("."));
            //七牛云上传图片
            String imgUrl = QiniuUtils.uploadForQiniu(imgFile.getInputStream(), newName);
            if(StringUtils.hasLength(imgUrl)){
                //上传图片成功后 将imgUrl存储到redis中
                redisUtils.sSet(RedisConstant.SETMEAL_PIC,imgUrl);
                return new Result(true, MessageConstant.UPLOAD_SUCCESS,imgUrl);
            }
        }
        return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
    }

    @PreAuthorize("hasRole('ADMIN') or hasAuthority('SETMEAL_ADD')")
    @ApiOperation("添加套餐信息")
    @PostMapping("/add")
    public Result add(
            @ApiParam("勾选的检查组id列表") @RequestParam("checkgroupIds") Integer[] checkgroupIds,
            @ApiParam("添加的套餐信息")@RequestBody Setmeal setmeal
    ){
        boolean flag  = setmealService.add(checkgroupIds,setmeal);
        if (flag){
            //新增套餐成功后 将套餐图片存储到redis中
            redisUtils.sSet(RedisConstant.SETMEAL_DB_PIC,setmeal.getImg());
            return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        }
        return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
    }

    @ApiOperation("根据条件分页查询套餐")
    @GetMapping("/findPage")
    public Result findPage(@ApiParam("分页查询条件") PageQueryVO vo){
        Page<Setmeal> page =  setmealService.findPage(vo);
        if (ObjectUtil.isNotEmpty(page)){
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,page);
        }
        return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
    }
}
